package lab1;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * <p>ReadXMLFile</p>
 * Reads an XML file into a vector
 * 
 * @author Anthony Varghese
 * 
 *
 */
public class ReadXMLFile {

	/**
	 * main method - where all the work in this class is done
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * External file to be read in
		 */
		final String DIRECTORY= "src/lab1/";
		final String FILENAME = "StateData.xml";
		final String COMPLETE_NAME = DIRECTORY + FILENAME;
		

		/*
		 * Set up the file for reading
		 */
		File infile = new File( COMPLETE_NAME );
		Document indoc = null;
		
		/*
		 * Open the file as an XML file and parse (read) the data within if possible
		 */
		try {
			DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			indoc = docbuilder.parse( infile );
		} catch (ParserConfigurationException e) {
			System.err.println("Not able to create DocumentBuilder object!");
			e.printStackTrace();
		} catch (SAXException e) {
			System.err.println("Not able to read XML file: " + COMPLETE_NAME );
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Not able to open file: " + COMPLETE_NAME );
			e.printStackTrace();
		}
		/*
		 * If everything above worked, we should have the contents of an XML file 
		 * converted to a "document" that we can inspect.
		 */
		
		/*
		 * Set up an XPath object so we can navigate the document.
		 */
		XPath path = XPathFactory.newInstance().newXPath();
		/*
		 * Things inside that XML file that we want to inspect:
		 */
		final String inspectString_root = "count(/usdata)";
		final String inspectString_states = "count(/usdata/statedata)";
		final String inspectString_7thState = "/usdata/statedata[7]/statename";
		final String inspectString_7thState_2000pop = "/usdata/statedata[7]/year[2]";
		final String inspectString_7thState_2009pop = "/usdata/statedata[7]/year[1]";

		
		if (indoc != null && path != null){
			/*
			 * Inspect the data we just read in
			 */
			System.out.println(" Analyzing data in XML file: ");
			try {
				int n = Integer.parseInt( path.evaluate(inspectString_root, indoc ));
				System.out.println(" number of countries in file is " + n );
				
				n = Integer.parseInt( path.evaluate(inspectString_states, indoc ));
				System.out.println(" number of states in root #1 of file is " + n );
				
				String s = path.evaluate(inspectString_7thState, indoc );
				System.out.println(" the 7th state in the list is: " + s);
				
				int pop = Integer.parseInt( path.evaluate( inspectString_7thState_2000pop , indoc ));
				System.out.println(" the population of " + s + " in 2000 was: " + pop);
				
				System.out.println(" the population of " + s + " in 2009 was: " + 
						Integer.parseInt( path.evaluate( inspectString_7thState_2009pop , indoc )));
			} catch (NumberFormatException e) {
				System.err.println("Not able to read number.");
				e.printStackTrace();
			} catch (XPathExpressionException e) {
				System.err.println("Bad XPath expression.");
				e.printStackTrace();
			}
			System.out.println(" ... done analyzing data in XML file.\n");
		} else {
			 System.err.println("Cannot inspect document!");
		}
	}

}
